package com.zto.tbschedule.console.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zto.tbschedule.console.help.LoginHelp;
import com.zto.tbschedule.console.service.IApplicationService;
import com.zto.tbschedule.console.util.ValidateUtil;
import com.zto.tbschedule.console.vo.AppUser;
import com.zto.tbschedule.console.vo.Application;
import com.zto.tbschedule.console.vo.Message;
import com.zto.tbschedule.console.vo.User;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/app")
public class ApplicationController {
    @Autowired
    private IApplicationService applicationService;
    @RequestMapping("/list.do")
    @ResponseBody
    public Message<List<Application>> listApp(HttpServletRequest request){
        User user = LoginHelp.getUser(request);
        /*if(!LoginHelp.isAdmin(user)){
            return new Message<List<Application>>(false,Message.ILLEGAL_OPERATION,"无权操作",null);
        }*/
        try{
            List<Application> appList = applicationService.getAppList();
            return new Message<List<Application>>(true,Message.SUCCESS,appList);
        }catch (Exception e){
            return new Message<List<Application>>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }
    }
    @RequestMapping("/add.do")
    @ResponseBody
    public Message<Void> addApp(HttpServletRequest request,Application app){
        User user = LoginHelp.getUser(request);
        /*if(!LoginHelp.isAdmin(user)){
            return new Message<Void>(false,Message.ILLEGAL_OPERATION,"无权操作",null);
        }*/
        app.setCreateId(user.getId());
        String result = ValidateUtil.validateModel(app);
        if(!StringUtils.isEmpty(result)){
            return new Message<Void>(false,Message.PROCESS_EXCEPTION,result,null);
        }
        try{
            applicationService.addApp(app);
            return new Message<Void>(true,Message.SUCCESS,"增加成功",null);
        }catch (Exception e){
            return new Message<Void>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }
    }

    @RequestMapping("/myapp.do")
    @ResponseBody
    public Message<List<AppUser>> myApp(HttpServletRequest request,Long userId){
        User user = LoginHelp.getUser(request);
        /*if(!LoginHelp.isAdmin(user)){
            return new Message<List<AppUser>>(false,Message.ILLEGAL_OPERATION,"无权操作",null);
        }*/
        try{
            List<AppUser> appList = applicationService.getAppListByUid(userId);
            return new Message<List<AppUser>>(true,Message.SUCCESS,appList);
        }catch (Exception e){
            return new Message<List<AppUser>>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }
    }
    @RequestMapping("/editMyApp.do")
    @ResponseBody
    public Message<Void> myApp(HttpServletRequest request,Long uid,Long aid,String type){
        User user = LoginHelp.getUser(request);
        if(!LoginHelp.isAdmin(user)){
            return new Message<Void>(false,Message.ILLEGAL_OPERATION,"无权操作",null);
        }
        try{
            applicationService.updateMyApp(uid,aid,type);
            return new Message<Void>(true,Message.SUCCESS,null);
        }catch (Exception e){
            return new Message<Void>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }
    }
}
